package com.tqjc.area.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.area.mapper.RollerMapper;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.service.RollerService;
import com.tqjc.execute.crane.entity.CraneInfo;
import com.tqjc.execute.crane.mapper.CraneInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 辊道信息Service实现
 * @author: pzy
 * @date: 2023年05月17日 10:45
 */

@Service
@DS("baotou")
public class RollerServiceImpl extends ServiceImpl<RollerMapper,Roller> implements RollerService {
    @Resource
    RollerMapper rollerMapper;

    @Resource
    CraneInfoMapper craneInfoMapper;

    /**
     * @param x:
     * @param y:
     * @return List<Roller>
     * @author Patienyon
     * @description 根据坐标获取对应辊道
     * @date 2023/6/16 15:43
     */
    @Override
    public List<Roller> selectRollersByXAndY(Integer x, Integer y) {
        return rollerMapper.selectRollersByXAndY(x,y);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Roller queryById(Integer id){
        return rollerMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param roller 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<Roller> paginQuery(Roller roller, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<Roller> queryWrapper = new LambdaQueryWrapper<>();
        /*if(StrUtil.isNotBlank(roller.getReadySignal())){
            queryWrapper.eq(Roller::getReadySignal, roller.getReadySignal());
        }
        if(StrUtil.isNotBlank(roller.getEmptySignal())){
            queryWrapper.eq(Roller::getEmptySignal, roller.getEmptySignal());
        }*/
        //2. 执行分页查询
        Page<Roller> pagin = new Page<>(current , size , true);
        IPage<Roller> selectResult = rollerMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param roller 实例对象
     * @return 实例对象
     */
    @Override
    public Roller insert(Roller roller){
        rollerMapper.insert(roller);
        return roller;
    }

    /**
     * 更新数据
     *
     * @param roller 实例对象
     * @return 实例对象
     */
    @Override
    public Roller update(Roller roller){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<Roller> chainWrapper = new LambdaUpdateChainWrapper<Roller>(rollerMapper);
        /*if(StrUtil.isNotBlank(roller.getReadySignal())){
            chainWrapper.eq(Roller::getReadySignal, roller.getReadySignal());
        }
        if(StrUtil.isNotBlank(roller.getEmptySignal())){
            chainWrapper.eq(Roller::getEmptySignal, roller.getEmptySignal());
        }*/
        //2. 设置主键，并更新
        chainWrapper.set(Roller::getId, roller.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(roller.getId());
        }else{
            return roller;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = rollerMapper.deleteById(id);
        return total > 0;
    }
    /**
     * 通过querymapper查询
     *
     * @param queryWrapper:查询条件
     * @return 辊道数据列表
     */
    @Override
    public List queryRoller(QueryWrapper queryWrapper) {
        return rollerMapper.selectList(queryWrapper);
    }


    @Override
    public List<Roller> selectRollerByCraneId(List<Integer> craneIds) {
        List<Roller> rollers = rollerMapper.selectRollerByCraneId(craneIds);
        return rollerMapper.selectRollerByCraneId(craneIds);
    }

    @Override
    public Roller getByName(String craneType) {
        CraneInfo craneInfo = craneInfoMapper.selectOne(new LambdaQueryWrapper<CraneInfo>().eq(CraneInfo::getName, craneType));
        return rollerMapper.selectById(craneInfo.getId());
    }
}
